//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmStepConditionComposite.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::CFdmStepConditionComposite (boost::shared_ptr<QuantLib::FdmStepConditionComposite>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmStepConditionComposite = NULL;
#endif
	_ppFdmStepConditionComposite = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::CFdmStepConditionComposite (QuantLib::FdmStepConditionComposite& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmStepConditionComposite = NULL;
#endif
	_ppFdmStepConditionComposite = new boost::shared_ptr<QuantLib::FdmStepConditionComposite> (&childNative);
    
    _FdmStepConditionCompositeOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::CFdmStepConditionComposite (CFdmStepConditionComposite^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmStepConditionComposite = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmStepConditionComposite = new boost::shared_ptr<QuantLib::FdmStepConditionComposite> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::CFdmStepConditionComposite (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmStepConditionComposite = NULL;
#endif
	if (!t->IsSubclassOf(CFdmStepConditionComposite::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::CFdmStepConditionComposite (QuantLib::Handle<QuantLib::FdmStepConditionComposite>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmStepConditionComposite = &childNative;
	_ppFdmStepConditionComposite = &static_cast<boost::shared_ptr<QuantLib::FdmStepConditionComposite>>(childNative.currentLink());
    
    _FdmStepConditionCompositeOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::CFdmStepConditionComposite (QuantLib::Handle<QuantLib::FdmStepConditionComposite> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmStepConditionComposite = &childNative;
	_ppFdmStepConditionComposite = &static_cast<boost::shared_ptr<QuantLib::FdmStepConditionComposite>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::CFdmStepConditionComposite (QuantLib::FdmStepConditionComposite childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmStepConditionComposite = NULL;
#endif
	_ppFdmStepConditionComposite = new boost::shared_ptr<QuantLib::FdmStepConditionComposite> (new QuantLib::FdmStepConditionComposite (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::~CFdmStepConditionComposite ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmStepConditionComposite != NULL)
    {
	    delete _ppFdmStepConditionComposite;
        _ppFdmStepConditionComposite = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::!CFdmStepConditionComposite ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmStepConditionComposite != NULL)
    {
	    delete _ppFdmStepConditionComposite;
    }
}
QuantLib::FdmStepConditionComposite& Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::GetReference ()
{
    if (_ppFdmStepConditionComposite == NULL) throw gcnew NativeNullException ();
	return **_ppFdmStepConditionComposite;
}
boost::shared_ptr<QuantLib::FdmStepConditionComposite>& Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::GetShared ()
{
    if (_ppFdmStepConditionComposite == NULL) throw gcnew NativeNullException ();
	return *_ppFdmStepConditionComposite;
}
QuantLib::FdmStepConditionComposite* Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::GetPointer ()
{
    if (_ppFdmStepConditionComposite == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmStepConditionComposite;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmStepConditionComposite>& Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::GetHandle ()
{
	if (_phFdmStepConditionComposite == NULL)
	{
		_phFdmStepConditionComposite = new Handle<QuantLib::FdmStepConditionComposite> (*_ppFdmStepConditionComposite);
	}
	return *_phFdmStepConditionComposite;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::HasNative () 
{
	return (_ppFdmStepConditionComposite != NULL);
}

Cephei::QL::Experimental::Finitedifferences::IFdmStepConditionComposite^ Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::ApplyTo (Cephei::QL::Math::IArray^ a, Double t)
{
    CArray^ _Ca;
    try
    {
        _Ca = safe_cast<CArray^> (a);
        _Ca->Lock();
        QuantLib::Array& _a = static_cast<QuantLib::Array&> (_Ca->GetReference ()); 
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	(*_ppFdmStepConditionComposite)->applyTo ( _a,  _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ca != nullptr) _Ca->Unlock();
    }
}
Cephei::IVector<Double>^ Cephei::QL::Experimental::Finitedifferences::CFdmStepConditionComposite::StoppingTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppFdmStepConditionComposite)->stoppingTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

